package com.situ.mall.controller;

import com.situ.mall.annotation.MyLog;
import com.situ.mall.pojo.Admin;
import com.situ.mall.pojo.query.AdminQuery;
import com.situ.mall.service.IAdminService;
import com.situ.mall.util.PageResult;
import com.situ.mall.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @RequestMapping("/login")
    public Result login(String username, String password, String code, HttpSession session) {
        System.out.println("AdminController.login");
        String codeInSession = (String) session.getAttribute("codeInSession");
        if(!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        System.out.println("用户名--------------" + username);
        Admin admin = adminService.login(username,password);
        if(admin == null){
            return Result.error("用户名或密码错误");
        }
        session.setAttribute("admin",admin);
            return Result.ok("登录成功");
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("admin");
        return Result.ok("退出成功");
    }

    @MyLog(module="分页")
    @RequestMapping("/list")
    public PageResult<Admin> list(AdminQuery adminQuery){
        PageResult<Admin> pageResult = adminService.list(adminQuery);
        return pageResult;
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        adminService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids){
        adminService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/add")
    public Result add(Admin admin){
        adminService.add(admin);
        return Result.ok("添加成功");
    }
}
